<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class LogService extends MY_Controller {
		public function __construct(){

		parent::__construct();

	}
	public  function conlog(){
		$this->load->helper('func');
		$this->load->database();
		$this->load->model('User_model','user');
		 $serv_ip = $_SERVER['SERVER_ADDR'];//服务器ip
		 $ip=$_SERVER['REMOTE_ADDR'];//客户端ip
		 
		if($serv_ip==$ip){
		$com = "cat /data/log/nginx/file_service_access.log |grep  /uploads/  |grep merCode=  |awk -F'[][]' '{print $16 $14 $20  $24}'";
		exec($com,$re);
		
		$arr=array();
		$hflow=array();
		$merc=array();//存放商户号
		foreach($re as $key=>$val){
			
			   $lentime = substr($val,0,5);//响应时间下载时间
			   $starttime= substr($val,5,19);//开始时间
			  $arr=explode('T',$starttime);
			  $strtime= "$arr[0] $arr[1]";//日期和时间
			  $h = substr($arr[1],0,2);
			 
			 $start=  strtotime($starttime) ;//开始时间戳
			  $endtime=strtotime($strtime)+intval($lentime);//结束时间
			  $strurl= substr($val,30);

			 $mer= substr($strurl,strpos($strurl,'=')+1);
			 $mui= substr($mer,strpos($mer,'=')+1);
			  $muid=substr($mui,0,strpos($mui,'&'));//muid
			 $mercode=substr($mer,0,strpos($mer,'&muid'));//商户号
			  $flow = substr($val,intval(strpos($val,'&sign='))+38);//流量
			 if(!in_array($mercode,$merc)){
				 $merc[]=$mercode; 
			 }
			
			 $file= substr($val,strrpos($val,'/')+1);
			 $filename=substr($file,0,strpos($file,'?'));//文件名
			 changeWide($flow);//带宽
			   $bandwide= bandWide($flow,$start,$endtime);//5分钟带宽
			
			  dayLog($mercode,$lentime,getRealSize($flow),changeWide($bandwide));//5分钟日志
			  commonLog($mercode,$muid,$filename,$starttime);
		}
		//查询不同的商户号并生成每小时下载日志文件
		foreach($merc as $key=>$val){
				 $daytotal=dayFlow($mercode);//每天流量统计生成日志并返回当天总流量
				  $daytotal =  round($daytotal/(1024*1024),2);	
				  
				 $this->user->dayflow($mercode,$daytotal,date('Y-m-d'));
		}
			echo    'ok';
		}else{
			echo  'fail';
		}
	}
	
}

